package com.diy.sigmund.diyuser.common.middleware;

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * <br>普通消费
 * <br>实现RocketMQListener接口
 * <br>申明泛型对象即业务对象
 * <br>使用@RocketMQMessageListener注解
 * <br>配置@RocketMQMessageListener注
 * <br>必须配置项入下
 * <br>group
 * <br>topic
 * <br>使用@Component注解或者子注解
 * <br>实现接口方法
 *
 * @author ylm-sigmund
 * @since 2022/5/5 22:21
 */
@Component
@RocketMQMessageListener(
        topic = "transactionMessage",
        consumerGroup = "transactionMessage-consumer")
public class ProductListener implements RocketMQListener<MessageExt> {

    private static final Logger log = LoggerFactory.getLogger(ProductListener.class);

    /**
     * 因为有 重试，这里如果是真实的业务需要自行实现 幂等性
     */
    @Override
    public void onMessage(MessageExt msg) {
        log.info("接收到事务消息msg:{}", msg);
        String property = msg.getProperty(RocketMQHeaders.TRANSACTION_ID);
        log.info("监听到下单消息, 商品服务减库存，orderId:{}", property);
    }

}
